package com.gxuwz.dx_qz_school;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.gxuwz.dx_qz_school.fragment.CardFragment;
import com.gxuwz.dx_qz_school.fragment.HomeFragment;
import com.gxuwz.dx_qz_school.fragment.MessageFragment;
import com.gxuwz.dx_qz_school.fragment.PersonFragment;

public class MainActivity extends AppCompatActivity {
    private TextView tv_home;
    private TextView tv_card;
    private TextView tv_message;
    private TextView tv_person;
    private HomeFragment homeFragment;
    private CardFragment cardFragment;
    private MessageFragment messageFragment;
    private PersonFragment personFragment;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //创建Fragment
        homeFragment = new HomeFragment();
        cardFragment = new CardFragment();
        messageFragment = new MessageFragment();
        personFragment = new PersonFragment();

        tv_home = findViewById(R.id.tv_home);
        tv_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_home.setTextColor(getColor(R.color.red));
                tv_card.setTextColor(getColor(R.color.black));
                tv_message.setTextColor(getColor(R.color.black));
                tv_person.setTextColor(getColor(R.color.black));
                setFragment(homeFragment);
            }
        });

        tv_card = findViewById(R.id.tv_card);
        tv_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_card.setTextColor(getColor(R.color.red));
                tv_home.setTextColor(getColor(R.color.black));
                tv_message.setTextColor(getColor(R.color.black));
                tv_person.setTextColor(getColor(R.color.black));
                setFragment(cardFragment);
            }
        });

        tv_message = findViewById(R.id.tv_message);
        tv_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_message.setTextColor(getColor(R.color.red));
                tv_card.setTextColor(getColor(R.color.black));
                tv_home.setTextColor(getColor(R.color.black));
                tv_person.setTextColor(getColor(R.color.black));
                setFragment(messageFragment);
            }
        });

        tv_person = findViewById(R.id.tv_person);
        tv_person.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_person.setTextColor(getColor(R.color.red));
                tv_card.setTextColor(getColor(R.color.black));
                tv_home.setTextColor(getColor(R.color.black));
                tv_message.setTextColor(getColor(R.color.black));
                setFragment(personFragment);
            }
        });

        // 设置第一个页面为HomeFragment
        setFragment(homeFragment);
        tv_home.setTextColor(getColor(R.color.red));

    }

    private void setFragment(Fragment fragment){
        if(fragment != null){
            fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.layout_fragment, fragment);
            fragmentTransaction.commit();
        }
    }
}